package org.thinker.web;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.thinker.member.MemberVO;

@Controller("memberController")
@RequestMapping("/member/"  )
public class MemberController {
	
	static Logger logger = Logger.getLogger(MemberController.class);
	
	

	@RequestMapping(value="registForm", method=RequestMethod.GET)
	public void registForm(Model model){
		
		model.addAttribute(new MemberVO());
		
		logger.info("registForm.............");
						
	}

	@RequestMapping(value="registForm", method=RequestMethod.POST)
	public String processSubmit(@Valid MemberVO vo, BindingResult result, Model model){
		
		logger.info("processSubmit.............");
		
		if(result.hasErrors()){
			
			logger.info("has errors");
			
			model.addAttribute("message", "Form has errors");
			
			return null;
		}
		
		model.addAttribute("result", "SUCCESS");
		
		return "success";
	}
	
	@RequestMapping(value="{memberid}.json")
	public @ResponseBody MemberVO sendJSON(@PathVariable("memberid")String memberId){
		
		MemberVO vo = new MemberVO();
		vo.setId(memberId);
		vo.setPw("pw0001");
		vo.setName("Sample User");
		
		return vo;
		
	}
	
	@RequestMapping(value="{memberid}.xml")
	public @ResponseBody MemberVO sendXML(@PathVariable("memberid")String memberId){
		
		MemberVO vo = new MemberVO();
		vo.setId(memberId);
		vo.setPw("xpw0001");
		vo.setName("XML Sample User");
		
		return vo;
		
	}	
	
}
